const http = require('http');
const fs = require('fs');
const mysql = require('mysql');
let config = {
    "host":'192.168.17.144',
    "user":"root",
    "password":"ddd",
    "database":"blog"
}
let con = mysql.createConnection(config);
con.connect();

http.createServer((req,res) =>{
    let myUrl = new URL(req.url,'http://localhost:8080')
    let pathName = myUrl.pathname;
    console.log(pathName);
    let filePath = '../chapterList.html';
    let juageArr = pathName.split('/');
    if (pathName == '/') {
        fs.readFile(filePath,(e,file) => {
            if (e) {
                console.log(e);
            }
            else{
                res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    
    else if (juageArr[1] == "css") {
        let cssFilePath='..'+pathName;
        fs.readFile(cssFilePath,(e,file) => {
            if (e) {
                console.log(e);
            }
            else{
                res.writeHead(200,{"Content-Type":"text/css;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1] == "js") {
        let jsFilePath='./'+juageArr[2];
        fs.readFile(jsFilePath,(e,file) => {
            if (e) {
                console.log(e);
            }
            else{
                res.writeHead(200,{"Content-Type":"text/plain;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1] == "images") {
        let jsFilePath='..'+pathName;
        fs.readFile(jsFilePath,(e,file) => {
            if (e) {
                res.end();
            }
            else{
                res.writeHead(200,{"Content-Type":"image/jpg;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1].indexOf('detail')!=-1) {
        let detailFilePath = '../chapter.html';
        fs.readFile(detailFilePath,(e,file) => {
            if (e) {
                res.end('404 Not Found');
            }
            else{
                res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1].indexOf('getDetail')!=-1) {
        console.log('Id',myUrl.search.split('=')[1])
        let needId = myUrl.search.split('=')[1];
        let sqltext = 'select * from blog where blogId=?';
        con.query(sqltext,[needId],(e,result) => {
            if (e) {
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                res.end('403');
            }
            else {
                let sendData = JSON.stringify(result);
                console.log('type:',typeof sendData)
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                res.end(sendData);
            }
        })
    }
    else if (juageArr[1]='/getChapterList') {
        let sqltext = 'select * from blog';
        con.query(sqltext,[],(e,result) => {
            if (e) {
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                res.end(JSON.stringify([]));
            }
            else{
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                let sendData = JSON.stringify(result);
                res.write(sendData);
                res.end();
            }
        });      
    }
    else{
        return
    }
}).listen(8080);
console.log("server is listening 8080")